﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using ESOL.Common.Entity;
using Suzsoft.Smart.Data;
using System.Data;
using ESOL.Common.DTO;
using ESOL.Business.Mst;
using ESOL.Common;

namespace ESOL.Services
{
    [ServiceBehavior(UseSynchronizationContext = false,
    ConcurrencyMode = ConcurrencyMode.Multiple,
    InstanceContextMode = InstanceContextMode.PerCall)]
    public class QcService : IQcService
    {
        public bool TestConnect()
        {
            return true;
        }

        //QC_ITEM 质检项目
        public List<QcItemEntity> GetQcItemAllList(bool isActive)
        {
            int active = 0;
            if (isActive)
            {
                active = 1;
            }
            return DataAccess.Select<QcItemEntity>(new QcItemEntity { IsActive = active }).ToList();
        }

        public QcItemQueryPageResult GetQcMaintainItemAllListByCondition(QcItemQueryCondition con)
        {
            int pageIndex = 1;
            int pageSize = 50;
            if (con.txtPageIndex != 0)
                pageIndex = con.txtPageIndex;
            if (con.txtPageSize != 0)
                pageSize = con.txtPageSize;
            string condition = "";
            if (con.ItemName != null)
            {
                condition += " and item.ITEM_NAME='" + con.ItemName + "'";
            }
            if (con.ItemCode != null)
            {
                condition += " and item.ITEM_CODE='" + con.ItemCode + "'";
            }
            if (con.IsActive != null)
            {
                condition += " and item.IS_ACTIVE='" + con.IsActive + "'";
            }
            if (con.Status != null)
            {
                condition += " and item.STATUS='" + con.Status + "'";
            }

            string sql = "";
            if (string.IsNullOrEmpty(condition))
            {
                string sqlSource = @"select top {0} *,(select count(1) from QC_ITEM) as cnt
from QC_ITEM
where id not in (
    select top ({1}*({2}-1)) id from QC_ITEM order by created_date desc
) order by created_date desc";
                sqlSource = string.Format(sqlSource, pageSize, pageSize, pageIndex);
                sql = @"select item.*,u1.firstName as CREATED_USER,u2.firstName as UPDATED_USER 
from ({0}) item
left join BPMDB.dbo.PW_User u1 on item.CREATED_BY=u1.userNo
left join BPMDB.dbo.PW_User u2 on item.CREATED_BY=u2.userNo";
                sql = string.Format(sql, sqlSource);
            }
            else
            {
                sql = @"select top {0} *,(select count(1) from ({3}) t) as cnt
from ({3}) t
where t.id not in (
    select top ({1}*({2}-1)) id from ({3}) t order by created_date desc
) order by created_date desc";
                string sqlSource = @"select item.*,u1.firstName as CREATED_USER,u2.firstName as UPDATED_USER 
from QC_ITEM item
left join BPMDB.dbo.PW_User u1 on item.CREATED_BY=u1.userNo
left join BPMDB.dbo.PW_User u2 on item.CREATED_BY=u2.userNo
where 1=1 ";
                sqlSource += condition;
                sql = string.Format(sql, pageSize, pageSize, pageIndex, sqlSource);
            }
            DataSet ds = DataAccess.SelectDataSet(sql);
            if (ds == null)
                return null;
            QcItemQueryPageResult result = new QcItemQueryPageResult();
            if (ds.Tables[0].Rows.Count == 0)
            {
                result.items = null;
                result.TotalCount = 0;
                return result;
            }
            List<QcItemEntity> results = new List<QcItemEntity>();
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                QcItemEntity cfg = new QcItemEntity(dr);
                results.Add(cfg);
            }
            result.items = results;
            result.TotalCount = (int)ds.Tables[0].Rows[0]["cnt"];
            return result;
        }

        public bool SaveQcItem(QcItemSaveParam param)
        {
            if (param == null)
                return false;
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();
                    if (param.delItems != null)
                    {
                        DataAccess.Delete(param.delItems, broker);
                    }
                    if (param.updatedItems != null)
                    {
                        foreach (var item in param.updatedItems)
                        {
                            item.UpdatedBy = param.userNo;
                            item.UpdatedDate = DateTime.Now;
                        }
                        DataAccess.Update(param.updatedItems, broker);
                    }
                    if (param.addItems != null)
                    {
                        foreach (var item in param.addItems)
                        {
                            item.CreatedBy = param.userNo;
                            item.CreatedDate = DateTime.Now;
                            item.UpdatedBy = param.userNo;
                            item.UpdatedDate = DateTime.Now;
                        }
                        DataAccess.Insert(param.addItems, broker);
                    }
                    broker.Commit();
                    return true;
                }
                catch (Exception ex)
                {
                    broker.RollBack();
                    return false;
                }
            }
        }

        //Config
        public QcConfigEntity GetQcMaintainConfigByKey(string configId)
        {
            if (string.IsNullOrEmpty(configId))
                return null;
            return DataAccess.SelectSingle<QcConfigEntity>(new QcConfigEntity { Id = configId });
        }

        public QcConfigEntity GetQcMaintainConfigByConfig(QcConfigEntity config)
        {
            if (config == null)
                return null;
            QcConfigEntity cfg = new QcConfigEntity();
            if (!string.IsNullOrEmpty(config.Id))
            {
                cfg.Id = config.Id;
            }
            if (!string.IsNullOrEmpty(config.CreatedBy))
            {
                cfg.CreatedBy = config.CreatedBy;
            }
            if (config.CreatedDate != null && config.CreatedDate != new DateTime(1, 1, 1))
            {
                cfg.CreatedDate = config.CreatedDate;
            }
            if (!string.IsNullOrEmpty(config.UpdatedBy))
            {
                cfg.UpdatedBy = config.UpdatedBy;
            }
            if (config.UpdatedDate != null && config.UpdatedDate != new DateTime(1, 1, 1))
            {
                cfg.UpdatedDate = config.UpdatedDate;
            }
            cfg.IsActive = config.IsActive;
            if (!string.IsNullOrEmpty(config.ConfigNo))
            {
                cfg.ConfigNo = config.ConfigNo;
            }
            if (!string.IsNullOrEmpty(config.MaterialId))
            {
                cfg.MaterialId = config.MaterialId;
            }
            if (!string.IsNullOrEmpty(config.MaterialName))
            {
                cfg.MaterialName = config.MaterialName;
            }
            if (!string.IsNullOrEmpty(config.MaterialCode))
            {
                cfg.MaterialCode = config.MaterialCode;
            }
            if (!string.IsNullOrEmpty(config.TypePur))
            {
                cfg.TypePur = config.TypePur;
            }
            if (!string.IsNullOrEmpty(config.TypeOther))
            {
                cfg.TypeOther = config.TypeOther;
            }
            if (!string.IsNullOrEmpty(config.TypeShipment))
            {
                cfg.TypeShipment = config.TypeShipment;
            }
            if (!string.IsNullOrEmpty(config.TypeFinishedgoods))
            {
                cfg.TypeFinishedgoods = config.TypeFinishedgoods;
            }
            if (!string.IsNullOrEmpty(config.TypeTechnic))
            {
                cfg.TypeTechnic = config.TypeTechnic;
            }
            if (!string.IsNullOrEmpty(config.TypeOut))
            {
                cfg.TypeOut = config.TypeOut;
            }
            if (!string.IsNullOrEmpty(config.Status))
            {
                cfg.Status = config.Status;
            }

            return DataAccess.SelectSingle<QcConfigEntity>(cfg);
        }

        public bool AddQcConfig(QcConfigEntity config)
        {
            if (config == null)
                return false;
            config.UpdatedDate = DateTime.Now;
            config.CreatedDate = DateTime.Now;
            config.ConfigNo = MstCodeRuleBs.GetCurrentOrderCode("QC_CFG");
            return DataAccess.Insert(config);
        }

        public bool UpdateQcConfig(QcConfigEntity config)
        {
            if (config == null)
                return false;
            config.UpdatedDate = DateTime.Now;
            return DataAccess.Update(config);
        }

        public bool DeleteQcConfig(QcConfigEntity config)
        {
            if (config == null)
                return false;
            return DataAccess.Delete(config);
        }

        public Common.DTO.ConfigQueryPageResult GetQcConfigAllListByCondition(Common.DTO.ConfigQueryCondition con)
        {
            int pageIndex = 1;
            int pageSize = 50;
            if (con.txtPageIndex != 0)
                pageIndex = con.txtPageIndex;
            if (con.txtPageSize != 0)
                pageSize = con.txtPageSize;
            string condition = "";
            if (!string.IsNullOrEmpty(con.CreatedBy))
            {
                condition += " and cfg.CREATED_BY='" + con.CreatedBy + "'";
            }
            else if (!string.IsNullOrEmpty(con.CreatedUser))
            {
                condition += " and u1.firstName like '%" + con.CreatedUser + "%'";
            }
            if (con.IsActive != null)
            {
                condition += " and cfg.IS_ACTIVE=" + con.IsActive;
            }
            if (!string.IsNullOrEmpty(con.ConfigNo))
            {
                condition += " and cfg.ConfigNo='" + con.ConfigNo + "'";
            }
            if (con.MaterialId != null)
            {
                condition += " and cfg.MATERIAL_ID='" + con.MaterialId + "'";
            }
            else if (con.MaterialName != null)
            {
                condition += " and cfg.MATERIAL_NAME like '%" + con.MaterialName + "%'";
            }
            if (con.Status != null)
            {
                condition += " and cfg.STATUS='" + con.Status + "'";
            }

            string sql = "";
            if (string.IsNullOrEmpty(condition))
            {
                string sqlSource = @"select top {0} *,(select count(1) from QC_CONFIG) as cnt
from QC_CONFIG
where id not in (
    select top ({1}*({2}-1)) id from QC_CONFIG order by created_date desc
) order by created_date desc";
                sqlSource = string.Format(sqlSource, pageSize, pageSize, pageIndex);
                sql = @"select cfg.*,u1.firstName as CREATED_USER,u2.firstName as UPDATED_USER 
from ({0}) cfg
left join BPMDB.dbo.PW_User u1 on cfg.CREATED_BY=u1.userNo
left join BPMDB.dbo.PW_User u2 on cfg.CREATED_BY=u2.userNo";
                sql = string.Format(sql, sqlSource);
            }
            else
            {
                sql = @"select top {0} *,(select count(1) from ({3}) t) as cnt
from ({3}) t
where t.id not in (
    select top ({1}*({2}-1)) id from ({3}) t order by created_date desc
) order by created_date desc";
                string sqlSource = @"select cfg.*,u1.firstName as CREATED_USER,u2.firstName as UPDATED_USER 
from QC_CONFIG cfg
left join BPMDB.dbo.PW_User u1 on cfg.CREATED_BY=u1.userNo
left join BPMDB.dbo.PW_User u2 on cfg.CREATED_BY=u2.userNo
where 1=1 ";
                sqlSource += condition;
                sql = string.Format(sql, pageSize, pageSize, pageIndex, sqlSource);
            }
            DataSet ds = DataAccess.SelectDataSet(sql);
            if (ds == null)
                return null;
            ConfigQueryPageResult result = new ConfigQueryPageResult();
            if (ds.Tables[0].Rows.Count == 0)
            {
                result.results = null;
                result.TotalCount = 0;
                return result;
            }
            List<QcConfigEntity> results = new List<QcConfigEntity>();
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                QcConfigEntity cfg = new QcConfigEntity(dr);
                results.Add(cfg);
            }
            result.results = results;
            result.TotalCount = (int)ds.Tables[0].Rows[0]["cnt"];
            return result;
        }

        public bool SaveQcConfig(QcConfigEntity config, string userNo)
        {
            if (config == null || string.IsNullOrEmpty(userNo))
                return false;
            var cur = DataAccess.SelectSingle<QcConfigEntity>(new QcConfigEntity { Id = config.Id });
            if (cur == null)//new
            {
                config.CreatedBy = userNo;
                config.CreatedDate = DateTime.Now;
                config.UpdatedBy = userNo;
                config.UpdatedDate = DateTime.Now;
                return DataAccess.Insert(config);
            }
            else
            {
                config.UpdatedBy = userNo;
                config.UpdatedDate = DateTime.Now;
                return DataAccess.Update(config);
            }
        }

        //Solution
        public QcSolutionEntity GetQcMaintainSolutionByKey(string solutionId)
        {
            if (string.IsNullOrEmpty(solutionId))
                return null;
            return DataAccess.SelectSingle<QcSolutionEntity>(new QcSolutionEntity { Id = solutionId });
        }

        public QcSolutionEntity GetQcMaintainSolutionBySolution(QcSolutionEntity solution)
        {
            if (solution == null)
                return null;
            QcSolutionEntity sol = new QcSolutionEntity();
            if (!string.IsNullOrEmpty(solution.Id))
            {
                sol.Id = solution.Id;
            }
            if (!string.IsNullOrEmpty(solution.CreatedBy))
            {
                sol.CreatedBy = solution.CreatedBy;
            }
            if (solution.CreatedDate != null && solution.CreatedDate != new DateTime(1, 1, 1))
            {
                sol.CreatedDate = solution.CreatedDate;
            }
            if (!string.IsNullOrEmpty(solution.UpdatedBy))
            {
                sol.UpdatedBy = solution.UpdatedBy;
            }
            if (solution.UpdatedDate != null && solution.UpdatedDate != new DateTime(1, 1, 1))
            {
                sol.UpdatedDate = solution.UpdatedDate;
            }
            sol.IsActive = solution.IsActive;
            if (!string.IsNullOrEmpty(solution.SolutionNo))
            {
                sol.SolutionNo = solution.SolutionNo;
            }
            if (!string.IsNullOrEmpty(solution.SolutionName))
            {
                sol.SolutionName = solution.SolutionName;
            }
            if (!string.IsNullOrEmpty(solution.MaterialId))
            {
                sol.MaterialId = solution.MaterialId;
            }
            if (!string.IsNullOrEmpty(solution.MaterialName))
            {
                sol.MaterialName = solution.MaterialName;
            }
            if (!string.IsNullOrEmpty(solution.MaterialCode))
            {
                sol.MaterialCode = solution.MaterialCode;
            }
            if (!string.IsNullOrEmpty(solution.Status))
            {
                sol.Status = solution.Status;
            }
            return DataAccess.SelectSingle<QcSolutionEntity>(sol);
        }

        public bool AddQcSolution(QcSolutionEntity solution)
        {
            if (solution == null)
                return false;
            solution.CreatedDate = DateTime.Now;
            solution.UpdatedDate = DateTime.Now;
            solution.SolutionNo = MstCodeRuleBs.GetCurrentOrderCode("QC_SOL");
            return DataAccess.Insert(solution);
        }

        public bool UpdateQcSolution(QcSolutionEntity solution)
        {
            if (solution == null)
                return false;
            solution.UpdatedDate = DateTime.Now;
            return DataAccess.Update(solution);
        }

        public bool DeleteQcSolution(QcSolutionEntity solution)
        {
            if (solution == null)
                return false;
            return DataAccess.Delete(solution);
        }

        public Common.DTO.SolutionQueryPageResult GetQcSolutionAllListByCondition(Common.DTO.SolutionQueryCondition con)
        {
            int pageIndex = 1;
            int pageSize = 50;
            if (con.txtPageIndex != 0)
                pageIndex = con.txtPageIndex;
            if (con.txtPageSize != 0)
                pageSize = con.txtPageSize;
            string condition = "";
            if (!string.IsNullOrEmpty(con.CreatedBy))
            {
                condition += " and sol.CREATED_BY='" + con.CreatedBy + "'";
            }
            else if (!string.IsNullOrEmpty(con.CreatedUser))
            {
                condition += " and u1.firstName like '%" + con.CreatedUser + "%'";
            }
            if (con.IsActive != null)
            {
                condition += " and sol.IS_ACTIVE=" + con.IsActive;
            }
            if (!string.IsNullOrEmpty(con.SolutionNo))
            {
                condition += " and sol.SOLUTION_NO='" + con.SolutionNo + "'";
            }
            if (!string.IsNullOrEmpty(con.SolutionName))
            {
                condition += " and sol.SOLUTION_NAME='" + con.SolutionName + "'";
            }
            if (con.MaterialId != null)
            {
                condition += " and sol.MATERIAL_ID='" + con.MaterialId + "'";
            }
            else if (con.MaterialName != null)
            {
                condition += " and sol.MATERIAL_NAME like '%" + con.MaterialName + "%'";
            }
            if (con.Status != null)
            {
                condition += " and sol.STATUS='" + con.Status + "'";
            }

            string sql = "";
            if (string.IsNullOrEmpty(condition))
            {
                string sqlSource = @"select top {0} *,(select count(1) from QC_SOLUTION) as cnt
from QC_SOLUTION
where id not in (
    select top ({1}*({2}-1)) id from QC_SOLUTION order by created_date desc
) order by created_date desc";
                sqlSource = string.Format(sqlSource, pageSize, pageSize, pageIndex);
                sql = @"select sol.*,u1.firstName as CREATED_USER,u2.firstName as UPDATED_USER 
from ({0}) sol
left join BPMDB.dbo.PW_User u1 on sol.CREATED_BY=u1.userNo
left join BPMDB.dbo.PW_User u2 on sol.CREATED_BY=u2.userNo";
                sql = string.Format(sql, sqlSource);
            }
            else
            {
                sql = @"select top {0} *,(select count(1) from ({3}) t) as cnt
from ({3}) t
where t.id not in (
    select top ({1}*({2}-1)) id from ({3}) t order by created_date desc
) order by created_date desc";
                string sqlSource = @"select sol.*,u1.firstName as CREATED_USER,u2.firstName as UPDATED_USER 
from QC_SOLUTION sol
left join BPMDB.dbo.PW_User u1 on sol.CREATED_BY=u1.userNo
left join BPMDB.dbo.PW_User u2 on sol.CREATED_BY=u2.userNo
where 1=1 ";
                sqlSource += condition;
                sql = string.Format(sql, pageSize, pageSize, pageIndex, sqlSource);
            }
            DataSet ds = DataAccess.SelectDataSet(sql);
            if (ds == null)
                return null;
            SolutionQueryPageResult result = new SolutionQueryPageResult();
            if (ds.Tables[0].Rows.Count == 0)
            {
                result.results = null;
                result.TotalCount = 0;
                return result;
            }
            List<QcSolutionEntity> results = new List<QcSolutionEntity>();
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                QcSolutionEntity sol = new QcSolutionEntity(dr);
                results.Add(sol);
            }
            result.results = results;
            result.TotalCount = (int)ds.Tables[0].Rows[0]["cnt"];
            return result;
        }

        public bool SaveSolution(QcSolutionEntity solution, List<QcSolutionItemEntity> items, string userNo)
        {
            if (solution == null || items == null || userNo == null)
                return false;
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    var cur = DataAccess.SelectSingle<QcSolutionEntity>(new QcSolutionEntity { Id = solution.Id });
                    if (cur == null)
                    {
                        broker.BeginTransaction();
                        solution.CreatedBy = userNo;
                        solution.CreatedDate = DateTime.Now;
                        solution.UpdatedBy = userNo;
                        solution.UpdatedDate = DateTime.Now;
                        DataAccess.Insert(solution, broker);
                        DataAccess.Insert(items, broker);
                        broker.Commit();
                        return true;
                    }
                    else
                    {
                        broker.BeginTransaction();
                        var curItems = DataAccess.Select<QcSolutionItemEntity>(new QcSolutionItemEntity { QcSolutionId = solution.Id }).ToList();
                        if (curItems != null)
                        {
                            DataAccess.Delete<QcSolutionItemEntity>(curItems, broker);
                        }
                        DataAccess.Update(solution, broker);
                        DataAccess.Insert(items, broker);
                        broker.Commit();
                        return true;
                    }
                }
                catch (Exception ex)
                {
                    broker.RollBack();
                    return false;
                }
            }
        }

        public List<QcSolutionItemEntity> GetQcSolutionItemAllListBySolutionId(string solutionId)
        {
            if (string.IsNullOrEmpty(solutionId))
                return null;
            return DataAccess.Select<QcSolutionItemEntity>(new QcSolutionItemEntity { QcSolutionId = solutionId });
        }

        public List<QcSolutionItemEntity> GetQcSolutionItemAllListBySolutionNo(string solutionNo)
        {
            if (string.IsNullOrEmpty(solutionNo))
                return null;
            QcSolutionEntity sol = DataAccess.SelectSingle<QcSolutionEntity>(new QcSolutionEntity { SolutionNo = solutionNo });
            if (sol == null)
                return null;
            return DataAccess.Select<QcSolutionItemEntity>(new QcSolutionItemEntity { QcSolutionId = sol.Id });

        }

        //抽样标准
        public QcSampleStdEntity GetQcMaintainSampleStdByKey(string id)
        {
            if (string.IsNullOrEmpty(id))
                return null;
            return DataAccess.SelectSingle<QcSampleStdEntity>(new QcSampleStdEntity { Id = id });
        }

        public bool AddQcSampleStd(QcSampleStdEntity std)
        {
            if (std == null)
                return false;
            std.CreatedDate = DateTime.Now;
            std.UpdatedDate = DateTime.Now;
            std.StdNo = MstCodeRuleBs.GetCurrentOrderCode("QC_STD");
            return DataAccess.Insert(std);
        }

        public bool UpdateQcSampleStd(QcSampleStdEntity std)
        {
            if (std == null)
                return false;
            std.UpdatedDate = DateTime.Now;
            return DataAccess.Update(std);
        }

        public bool DeleteQcSampleStd(QcSampleStdEntity std)
        {
            if (std == null)
                return false;
            return DataAccess.Delete(std);
        }

        public Common.DTO.StdQueryPageResult GetQcSampleStdAllListByCondition(Common.DTO.StdQueryCondition con)
        {
            int pageIndex = 1;
            int pageSize = 50;
            if (con.txtPageIndex != 0)
                pageIndex = con.txtPageIndex;
            if (con.txtPageSize != 0)
                pageSize = con.txtPageSize;
            string condition = "";
            if (!string.IsNullOrEmpty(con.CreatedBy))
            {
                condition += " and std.CREATED_BY='" + con.CreatedBy + "'";
            }
            else if (!string.IsNullOrEmpty(con.CreatedUser))
            {
                condition += " and u1.firstName like '%" + con.CreatedUser + "%'";
            }
            condition += " and std.IS_ACTIVE=" + con.IsActive;
            if (!string.IsNullOrEmpty(con.StdNo))
            {
                condition += " and sol.STD_NO='" + con.StdNo + "'";
            }
            if (!string.IsNullOrEmpty(con.StdName))
            {
                condition += " and sol.STD_NAME='" + con.StdName + "'";
            }
            if (con.SampleFunction != null)
            {
                condition += " and sol.SAMPLE_FUNCTION='" + con.SampleFunction + "'";
            }
            if (con.Status != null)
            {
                condition += " and sol.STATUS='" + con.Status + "'";
            }

            string sql = "";
            if (string.IsNullOrEmpty(condition))
            {
                string sqlSource = @"select top {0} *,(select count(1) from QC_SAMPLE_STD) as cnt
from QC_SAMPLE_STD
where id not in (
    select top ({1}*({2}-1)) id from QC_SAMPLE_STD order by created_date desc
) order by created_date desc";
                sqlSource = string.Format(sqlSource, pageSize, pageSize, pageIndex);
                sql = @"select std.*,u1.firstName as CREATED_USER,u2.firstName as UPDATED_USER 
from ({0}) std
left join BPMDB.dbo.PW_User u1 on std.CREATED_BY=u1.userNo
left join BPMDB.dbo.PW_User u2 on std.CREATED_BY=u2.userNo";
                sql = string.Format(sql, sqlSource);
            }
            else
            {
                sql = @"select top {0} *,(select count(1) from ({3}) t) as cnt
from ({3}) t
where t.id not in (
    select top ({1}*({2}-1)) id from ({3}) t order by created_date desc
) order by created_date desc";
                string sqlSource = @"select std.*,u1.firstName as CREATED_USER,u2.firstName as UPDATED_USER 
from QC_SAMPLE_STD std
left join BPMDB.dbo.PW_User u1 on std.CREATED_BY=u1.userNo
left join BPMDB.dbo.PW_User u2 on std.CREATED_BY=u2.userNo
where 1=1 ";
                sqlSource += condition;
                sql = string.Format(sql, pageSize, pageSize, pageIndex, sqlSource);
            }
            DataSet ds = DataAccess.SelectDataSet(sql);
            if (ds == null)
                return null;
            StdQueryPageResult result = new StdQueryPageResult();
            if (ds.Tables[0].Rows.Count == 0)
            {
                result.results = null;
                result.TotalCount = 0;
                return result;
            }
            List<QcSampleStdEntity> results = new List<QcSampleStdEntity>();
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                QcSampleStdEntity sol = new QcSampleStdEntity(dr);
                results.Add(sol);
            }
            result.results = results;
            result.TotalCount = (int)ds.Tables[0].Rows[0]["cnt"];
            return result;
        }

        public bool SaveQcSampleStd(QcSampleStdEntity std, string userNo)
        {
            if (std == null || userNo == null)
                return false;
            var cur = DataAccess.SelectSingle<QcSampleStdEntity>(new QcSampleStdEntity { Id = std.Id });
            if (cur == null)//new
            {
                std.CreatedBy = userNo;
                std.CreatedDate = DateTime.Now;
                std.UpdatedBy = userNo;
                std.UpdatedDate = DateTime.Now;
                return DataAccess.Insert(std);
            }
            else
            {
                std.UpdatedBy = userNo;
                std.UpdatedDate = DateTime.Now;
                return DataAccess.Update(std);
            }
        }

        //IQC采购来料质检
        public QcIqcEntity GetQcIqcByKey(string id)
        {
            if (string.IsNullOrEmpty(id))
                return null;
            return DataAccess.SelectSingle<QcIqcEntity>(new QcIqcEntity { Id = id });
        }

        public bool AddQcIqc(QcIqcEntity iqc)
        {
            if (iqc == null)
                return false;
            iqc.CreatedDate = DateTime.Now;
            iqc.UpdatedDate = DateTime.Now;
            iqc.IqcNo = MstCodeRuleBs.GetCurrentOrderCode("QC_IQC");
            return DataAccess.Insert(iqc);
        }

        public bool UpdateQcIqc(QcIqcEntity iqc)
        {
            if (iqc == null)
                return false;
            iqc.UpdatedDate = DateTime.Now;
            return DataAccess.Update(iqc);
        }

        public bool DeleteQcIqc(QcIqcEntity iqc)
        {
            if (iqc == null)
                return false;
            return DataAccess.Delete(iqc);
        }

        public Common.DTO.IqcQueryPageResult GetQcIqcAllListByCondition(Common.DTO.IqcQueryCondition con)
        {
            int pageIndex = 1;
            int pageSize = 50;
            if (con.txtPageIndex != 0)
                pageIndex = con.txtPageIndex;
            if (con.txtPageSize != 0)
                pageSize = con.txtPageSize;
            string condition = "";
            if (!string.IsNullOrEmpty(con.CreatedBy))
            {
                condition += " and iqc.CREATED_BY='" + con.CreatedBy + "'";
            }
            else if (!string.IsNullOrEmpty(con.CreatedUser))
            {
                condition += " and u1.firstName like '%" + con.CreatedUser + "%'";
            }
            condition += " and iqc.IS_ACTIVE=" + con.IsActive;
            if (!string.IsNullOrEmpty(con.IqcNo))
            {
                condition += " and iqc.IQC_NO='" + con.IqcNo + "'";
            }
            if (!string.IsNullOrEmpty(con.PurArvNo))
            {
                condition += " and iqc.PUR_ARV_NO='" + con.PurArvNo + "'";
            }
            if (!string.IsNullOrEmpty(con.PoNo))
            {
                condition += " and iqc.PO_NO='" + con.PoNo + "'";
            }
            if (!string.IsNullOrEmpty(con.MaterialId))
            {
                condition += " and iqc.MATERIAL_ID='" + con.MaterialId + "'";
            }
            else if (!string.IsNullOrEmpty(con.MaterialName))
            {
                condition += " and iqc.MATERIAL_NAME='" + con.MaterialName + "'";
            }
            if (!string.IsNullOrEmpty(con.Type))
            {
                condition += " and iqc.TYPE='" + con.Type + "'";
            }
            if (!string.IsNullOrEmpty(con.SolutionNo))
            {
                condition += " and iqc.SOLUTION_NO='" + con.SolutionNo + "'";
            }
            if (!string.IsNullOrEmpty(con.SampleStd))
            {
                condition += " and iqc.SAMPLE_STD='" + con.SampleStd + "'";
            }
            if (con.Status != null)
            {
                condition += " and iqc.STATUS='" + con.Status + "'";
            }

            string sql = "";
            if (string.IsNullOrEmpty(condition))
            {
                string sqlSource = @"select top {0} *,(select count(1) from QC_IQC) as cnt
from QC_IQC
where id not in (
    select top ({1}*({2}-1)) id from QC_IQC order by created_date desc
) order by created_date desc";
                sqlSource = string.Format(sqlSource, pageSize, pageSize, pageIndex);
                sql = @"select iqc.*,u1.firstName as CREATED_USER,u2.firstName as UPDATED_USER 
from ({0}) iqc
left join BPMDB.dbo.PW_User u1 on iqc.CREATED_BY=u1.userNo
left join BPMDB.dbo.PW_User u2 on iqc.CREATED_BY=u2.userNo";
                sql = string.Format(sql, sqlSource);
            }
            else
            {
                sql = @"select top {0} *,(select count(1) from ({3}) t) as cnt
from ({3}) t
where t.id not in (
    select top ({1}*({2}-1)) id from ({3}) t order by created_date desc
) order by created_date desc";
                string sqlSource = @"select iqc.*,u1.firstName as CREATED_USER,u2.firstName as UPDATED_USER 
from QC_IQC iqc
left join BPMDB.dbo.PW_User u1 on iqc.CREATED_BY=u1.userNo
left join BPMDB.dbo.PW_User u2 on iqc.CREATED_BY=u2.userNo
where 1=1 ";
                sqlSource += condition;
                sql = string.Format(sql, pageSize, pageSize, pageIndex, sqlSource);
            }
            DataSet ds = DataAccess.SelectDataSet(sql);
            if (ds == null)
                return null;
            IqcQueryPageResult result = new IqcQueryPageResult();
            if (ds.Tables[0].Rows.Count == 0)
            {
                result.results = null;
                result.TotalCount = 0;
                return result;
            }
            List<QcIqcEntity> results = new List<QcIqcEntity>();
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                QcIqcEntity sol = new QcIqcEntity(dr);
                results.Add(sol);
            }
            result.results = results;
            result.TotalCount = (int)ds.Tables[0].Rows[0]["cnt"];
            return result;
        }

        public bool AddQcIqcRecord(QcIqcRecordEntity e)
        {
            if (e == null)
                return false;
            return DataAccess.Insert(e);
        }

        public bool UpdateQcIqcRecord(QcIqcRecordEntity e)
        {
            if (e == null)
                return false;
            return DataAccess.Update(e);
        }

        public bool DeleteQcIqcRecord(QcIqcRecordEntity e)
        {
            if (e == null)
                return false;
            return DataAccess.Delete(e);
        }

        public List<QcIqcRecordEntity> GetQcIqcRecordAllListByIqcNo(string iqcNo)
        {
            if (string.IsNullOrEmpty(iqcNo))
                return null;
            return DataAccess.Select<QcIqcRecordEntity>(new QcIqcRecordEntity { IqcNo = iqcNo }).ToList();
        }

        public bool SaveQcIqc(Common.DTO.IqcSaveParam param)
        {
            if (param == null)
                return false;
            if (param.iqc == null)
                return false;
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    QcIqcEntity cur = DataAccess.SelectSingle<QcIqcEntity>(new QcIqcEntity { Id = param.iqc.Id });
                    if (cur == null)//新增
                    {
                        param.iqc.CreatedDate = DateTime.Now;
                        param.iqc.CreatedBy = param.curUserNo;
                        param.iqc.UpdatedDate = DateTime.Now;
                        param.iqc.UpdatedBy = param.curUserNo;
                        param.iqc.IqcNo = MstCodeRuleBs.GetCurrentOrderCode("QC_IQC");
                        foreach (var iqci in param.addRecords)
                        {
                            iqci.CreatedBy = param.iqc.CreatedBy;
                            iqci.CreatedDate = param.iqc.CreatedDate;
                            iqci.IqcNo = param.iqc.IqcNo;
                        }
                        DataAccess.Insert(param.iqc, broker);
                        DataAccess.Insert(param.addRecords, broker);
                        broker.Commit();
                        return true;
                    }
                    else//修改
                    {
                        param.iqc.UpdatedDate = DateTime.Now;
                        foreach (var iqci in param.addRecords)
                        {
                            iqci.CreatedBy = param.iqc.CreatedBy;
                            iqci.CreatedDate = param.iqc.CreatedDate;
                            iqci.IqcNo = param.iqc.IqcNo;
                        }
                        DataAccess.Update(param.iqc, broker);
                        DataAccess.Delete(param.delRecords, broker);
                        DataAccess.Update(param.updatedRecords, broker);
                        DataAccess.Insert(param.addRecords, broker);
                        broker.Commit();
                        return true;
                    }
                }
                catch (Exception ex)
                {
                    broker.RollBack();
                    return false;
                }
            }
        }

        //IQC,工作流：到货，质检完成创建入库申请单
        public InvIappEntity QcIqcToInvIapp(string userNo, QcIqcEntity iqc)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    if (iqc == null || string.IsNullOrEmpty(userNo))
                        return null;
                    InvIappEntity app = new InvIappEntity();
                    app.Id = Guid.NewGuid().ToString();
                    app.CreatedBy = userNo;
                    app.CreatedDate = DateTime.Now;
                    app.UpdatedBy = userNo;
                    app.UpdatedDate = DateTime.Now;
                    app.IsActive = 1;
                    app.AppNo = MstCodeRuleBs.GetCurrentOrderCode("InvInAppOrder");
                    app.Type = (int)InvInType.采购质检入库;
                    app.RelatedId = iqc.Id;
                    app.Status = WfStatus.WFS10.ToString();

                    InvIappiEntity item = new InvIappiEntity();
                    item.Id = Guid.NewGuid().ToString();
                    item.IoAppId = app.Id;
                    item.ProductId = iqc.MaterialId;
                    item.Qty = iqc.AcptQty;

                    iqc.Status = WfStatus.WFS20.ToString();

                    broker.BeginTransaction();
                    DataAccess.Insert(app, broker);
                    DataAccess.Insert(item, broker);
                    DataAccess.Update(iqc, broker);
                    broker.Commit();
                    return app;
                }
                catch (Exception ex)
                {
                    broker.RollBack();
                    return null;
                }
            }
        }

        //SPQC，发货质检
        public QcSpqcEntity GetQcSpqcByKey(string id)
        {
            if (string.IsNullOrEmpty(id))
                return null;
            return DataAccess.SelectSingle<QcSpqcEntity>(new QcSpqcEntity { Id = id });
        }

        public bool AddQcSpqc(QcSpqcEntity spqc)
        {
            if (spqc == null)
                return false;
            spqc.SpqcNo = MstCodeRuleBs.GetCurrentOrderCode("QC_SPQC");
            spqc.CreatedDate = DateTime.Now;
            spqc.UpdatedDate = DateTime.Now;
            return DataAccess.Insert(spqc);
        }

        public bool UpdateQcSpqc(QcSpqcEntity spqc)
        {
            if (spqc == null)
                return false;
            spqc.UpdatedDate = DateTime.Now;
            return DataAccess.Insert(spqc);
        }

        public bool DeleteQcSpqc(QcSpqcEntity spqc)
        {
            if (spqc == null)
                return false;
            return DataAccess.Insert(spqc);
        }

        public Common.DTO.SpqcQueryPageResult GetQcSpqcAllListByCondition(Common.DTO.SpqcQueryCondition con)
        {
            int pageIndex = 1;
            int pageSize = 50;
            if (con.txtPageIndex != 0)
                pageIndex = con.txtPageIndex;
            if (con.txtPageSize != 0)
                pageSize = con.txtPageSize;
            string condition = "";
            if (!string.IsNullOrEmpty(con.CreatedBy))
            {
                condition += " and spqc.CREATED_BY='" + con.CreatedBy + "'";
            }
            else if (!string.IsNullOrEmpty(con.CreatedUser))
            {
                condition += " and u1.firstName like '%" + con.CreatedUser + "%'";
            }
            condition += " and spqc.IS_ACTIVE=" + con.IsActive;
            if (!string.IsNullOrEmpty(con.SpqcNo))
            {
                condition += " and spqc.SPQC_NO='" + con.SpqcNo + "'";
            }
            if (!string.IsNullOrEmpty(con.SalesSpNo))
            {
                condition += " and spqc.SALES_SP_NO='" + con.SalesSpNo + "'";
            }
            if (!string.IsNullOrEmpty(con.MaterialId))
            {
                condition += " and spqc.MATERIAL_ID='" + con.MaterialId + "'";
            }
            else if (!string.IsNullOrEmpty(con.MaterialName))
            {
                condition += " and spqc.MATERIAL_NAME like '%" + con.MaterialName + "%'";
            }
            if (!string.IsNullOrEmpty(con.Type))
            {
                condition += " and spqc.TYPE='" + con.Type + "'";
            }
            if (!string.IsNullOrEmpty(con.SolutionNo))
            {
                condition += " and spqc.SOLUTION_NO='" + con.SolutionNo + "'";
            }
            if (!string.IsNullOrEmpty(con.SampleStd))
            {
                condition += " and spqc.SAMPLE_STD='" + con.SampleStd + "'";
            }
            if (!string.IsNullOrEmpty(con.WhNo))
            {
                condition += " and spqc.WH_NO='" + con.WhNo + "'";
            }
            if (con.Status != null)
            {
                condition += " and spqc.STATUS='" + con.Status + "'";
            }

            string sql = "";
            if (string.IsNullOrEmpty(condition))
            {
                string sqlSource = @"select top {0} *,(select count(1) from QC_SPQC) as cnt
from QC_SPQC
where id not in (
    select top ({1}*({2}-1)) id from QC_SPQC order by created_date desc
) order by created_date desc";
                sqlSource = string.Format(sqlSource, pageSize, pageSize, pageIndex);
                sql = @"select spqc.*,u1.firstName as CREATED_USER,u2.firstName as UPDATED_USER 
from ({0}) spqc
left join BPMDB.dbo.PW_User u1 on spqc.CREATED_BY=u1.userNo
left join BPMDB.dbo.PW_User u2 on spqc.CREATED_BY=u2.userNo";
                sql = string.Format(sql, sqlSource);
            }
            else
            {
                sql = @"select top {0} *,(select count(1) from ({3}) t) as cnt
from ({3}) t
where t.id not in (
    select top ({1}*({2}-1)) id from ({3}) t order by created_date desc
) order by created_date desc";
                string sqlSource = @"select spqc.*,u1.firstName as CREATED_USER,u2.firstName as UPDATED_USER 
from QC_SPQC spqc
left join BPMDB.dbo.PW_User u1 on spqc.CREATED_BY=u1.userNo
left join BPMDB.dbo.PW_User u2 on spqc.CREATED_BY=u2.userNo
where 1=1 ";
                sqlSource += condition;
                sql = string.Format(sql, pageSize, pageSize, pageIndex, sqlSource);
            }
            DataSet ds = DataAccess.SelectDataSet(sql);
            if (ds == null)
                return null;
            SpqcQueryPageResult result = new SpqcQueryPageResult();
            if (ds.Tables[0].Rows.Count == 0)
            {
                result.results = null;
                result.TotalCount = 0;
                return result;
            }
            List<QcSpqcEntity> results = new List<QcSpqcEntity>();
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                QcSpqcEntity sol = new QcSpqcEntity(dr);
                results.Add(sol);
            }
            result.results = results;
            result.TotalCount = (int)ds.Tables[0].Rows[0]["cnt"];
            return result;
        }

        public bool AddQcSpqcRecord(QcSpqcRecordEntity e)
        {
            if (e == null)
                return false;
            e.CreatedDate = DateTime.Now;
            return DataAccess.Insert(e);
        }

        public bool UpdateQcSpqcRecord(QcSpqcRecordEntity e)
        {
            if (e == null)
                return false;
            return DataAccess.Update(e);
        }

        public bool DeleteQcSpqcRecord(QcSpqcRecordEntity e)
        {
            if (e == null)
                return false;
            return DataAccess.Delete(e);
        }

        public List<QcSpqcRecordEntity> GetQcSpqcRecordAllListBySpqcNo(string SpqcNo)
        {
            if (string.IsNullOrEmpty(SpqcNo))
                return null;
            return DataAccess.Select<QcSpqcRecordEntity>(new QcSpqcRecordEntity { SpqcNo = SpqcNo });
        }

        public bool SaveQcSpqc(Common.DTO.SpqcSaveParam param)
        {
            if (param == null)
                return false;
            if (param.spqc == null)
                return false;
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    QcSpqcEntity cur = DataAccess.SelectSingle<QcSpqcEntity>(new QcSpqcEntity { Id = param.spqc.Id });
                    if (cur == null)//新增
                    {
                        param.spqc.CreatedDate = DateTime.Now;
                        param.spqc.UpdatedDate = DateTime.Now;
                        param.spqc.SpqcNo = MstCodeRuleBs.GetCurrentOrderCode("QC_SPQC");
                        foreach (var iqci in param.addRecords)
                        {
                            iqci.CreatedBy = param.spqc.CreatedBy;
                            iqci.CreatedDate = param.spqc.CreatedDate;
                            iqci.SpqcNo = param.spqc.SpqcNo;
                        }
                        DataAccess.Insert(param.spqc, broker);
                        DataAccess.Insert(param.addRecords, broker);
                        broker.Commit();
                        return true;
                    }
                    else//修改
                    {
                        param.spqc.UpdatedDate = DateTime.Now;
                        foreach (var iqci in param.addRecords)
                        {
                            iqci.CreatedBy = param.spqc.CreatedBy;
                            iqci.CreatedDate = param.spqc.CreatedDate;
                            iqci.SpqcNo = param.spqc.SpqcNo;
                        }
                        DataAccess.Update(param.spqc, broker);
                        DataAccess.Delete(param.delRecords, broker);
                        DataAccess.Update(param.updatedRecords, broker);
                        DataAccess.Insert(param.addRecords, broker);
                        broker.Commit();
                        return true;
                    }
                }
                catch (Exception ex)
                {
                    broker.RollBack();
                    return false;
                }
            }
        }

        //IPQC,过程检验
        public QcIpqcEntity GetQcIpqcByKey(string id)
        {
            if (string.IsNullOrEmpty(id))
                return null;
            return DataAccess.SelectSingle<QcIpqcEntity>(new QcIpqcEntity { Id = id });
        }

        public bool AddQcIpqc(QcIpqcEntity ipqc)
        {
            if (ipqc == null)
                return false;
            ipqc.IpqcNo = MstCodeRuleBs.GetCurrentOrderCode("QC_IPQC");
            ipqc.CreatedDate = DateTime.Now;
            ipqc.UpdatedDate = DateTime.Now;
            return DataAccess.Insert(ipqc);
        }

        public bool UpdateQcIpqc(QcIpqcEntity ipqc)
        {
            if (ipqc == null)
                return false;
            ipqc.UpdatedDate = DateTime.Now;
            return DataAccess.Update(ipqc);
        }

        public bool DeleteQcIpqc(QcIpqcEntity ipqc)
        {
            if (ipqc == null)
                return false;
            return DataAccess.Delete(ipqc);
        }

        public Common.DTO.IpqcQueryPageResult GetQcIpqcAllListByCondition(Common.DTO.IpqcQueryCondition con)
        {
            int pageIndex = 1;
            int pageSize = 50;
            if (con.txtPageIndex != 0)
                pageIndex = con.txtPageIndex;
            if (con.txtPageSize != 0)
                pageSize = con.txtPageSize;
            string condition = "";
            if (!string.IsNullOrEmpty(con.CreatedBy))
            {
                condition += " and ipqc.CREATED_BY='" + con.CreatedBy + "'";
            }
            else if (!string.IsNullOrEmpty(con.CreatedUser))
            {
                condition += " and u1.firstName like '%" + con.CreatedUser + "%'";
            }
            condition += " and spqc.IS_ACTIVE=" + con.IsActive;
            if (!string.IsNullOrEmpty(con.IpqcNo))
            {
                condition += " and ipqc.IPQC_NO='" + con.IpqcNo + "'";
            }
            if (!string.IsNullOrEmpty(con.WoNo))
            {
                condition += " and ipqc.WO_NO='" + con.WoNo + "'";
            }
            if (!string.IsNullOrEmpty(con.TechnicName))
            {
                condition += " and ipqc.TECHNIC_NAME like '%" + con.TechnicName + "%'";
            }
            if (!string.IsNullOrEmpty(con.MaterialId))
            {
                condition += " and ipqc.MATERIAL_ID='" + con.MaterialId + "'";
            }
            else if (!string.IsNullOrEmpty(con.MaterialName))
            {
                condition += " and ipqc.MATERIAL_NAME='" + con.MaterialName + "'";
            }
            if (!string.IsNullOrEmpty(con.Type))
            {
                condition += " and ipqc.TYPE='" + con.Type + "'";
            }
            if (!string.IsNullOrEmpty(con.SolutionNo))
            {
                condition += " and ipqc.SOLUTION_NO='" + con.SolutionNo + "'";
            }
            if (!string.IsNullOrEmpty(con.SampleStd))
            {
                condition += " and ipqc.SAMPLE_STD='" + con.SampleStd + "'";
            }
            if (con.Status != null)
            {
                condition += " and spqc.STATUS='" + con.Status + "'";
            }

            string sql = "";
            if (string.IsNullOrEmpty(condition))
            {
                string sqlSource = @"select top {0} *,(select count(1) from QC_IPQC) as cnt
from QC_IPQC
where id not in (
    select top ({1}*({2}-1)) id from QC_IPQC order by created_date desc
) order by created_date desc";
                sqlSource = string.Format(sqlSource, pageSize, pageSize, pageIndex);
                sql = @"select ipqc.*,u1.firstName as CREATED_USER,u2.firstName as UPDATED_USER 
from ({0}) ipqc
left join BPMDB.dbo.PW_User u1 on ipqc.CREATED_BY=u1.userNo
left join BPMDB.dbo.PW_User u2 on ipqc.CREATED_BY=u2.userNo";
                sql = string.Format(sql, sqlSource);
            }
            else
            {
                sql = @"select top {0} *,(select count(1) from ({3}) t) as cnt
from ({3}) t
where t.id not in (
    select top ({1}*({2}-1)) id from ({3}) t order by created_date desc
) order by created_date desc";
                string sqlSource = @"select ipqc.*,u1.firstName as CREATED_USER,u2.firstName as UPDATED_USER 
from QC_IPQC ipqc
left join BPMDB.dbo.PW_User u1 on ipqc.CREATED_BY=u1.userNo
left join BPMDB.dbo.PW_User u2 on ipqc.CREATED_BY=u2.userNo
where 1=1 ";
                sqlSource += condition;
                sql = string.Format(sql, pageSize, pageSize, pageIndex, sqlSource);
            }
            DataSet ds = DataAccess.SelectDataSet(sql);
            if (ds == null)
                return null;
            IpqcQueryPageResult result = new IpqcQueryPageResult();
            if (ds.Tables[0].Rows.Count == 0)
            {
                result.results = null;
                result.TotalCount = 0;
                return result;
            }
            List<QcIpqcEntity> results = new List<QcIpqcEntity>();
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                QcIpqcEntity sol = new QcIpqcEntity(dr);
                results.Add(sol);
            }
            result.results = results;
            result.TotalCount = (int)ds.Tables[0].Rows[0]["cnt"];
            return result;
        }

        public bool AddQcIpqcRecord(QcIpqcRecordEntity e)
        {
            if (e == null)
                return false;
            e.CreatedDate = DateTime.Now;
            return DataAccess.Insert(e);
        }

        public bool UpdateQcIpqcRecord(QcIpqcRecordEntity e)
        {
            if (e == null)
                return false;
            return DataAccess.Update(e);
        }

        public bool DeleteQcIpqcRecord(QcIpqcRecordEntity e)
        {
            if (e == null)
                return false;
            return DataAccess.Delete(e);
        }

        public List<QcIpqcRecordEntity> GetQcIpqcRecordAllListByIpqcNo(string IpqcNo)
        {
            if (string.IsNullOrEmpty(IpqcNo))
                return null;
            return DataAccess.Select<QcIpqcRecordEntity>(new QcIpqcRecordEntity { IpqcNo = IpqcNo }).ToList();
        }

        public bool SaveQcIpqc(Common.DTO.IpqcSaveParam param)
        {
            if (param == null)
                return false;
            if (param.ipqc == null)
                return false;
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    QcSpqcEntity cur = DataAccess.SelectSingle<QcSpqcEntity>(new QcSpqcEntity { Id = param.ipqc.Id });
                    if (cur == null)//新增
                    {
                        param.ipqc.CreatedDate = DateTime.Now;
                        param.ipqc.UpdatedDate = DateTime.Now;
                        param.ipqc.IpqcNo = MstCodeRuleBs.GetCurrentOrderCode("QC_IPQC");
                        foreach (var iqci in param.addRecords)
                        {
                            iqci.CreatedBy = param.ipqc.CreatedBy;
                            iqci.CreatedDate = param.ipqc.CreatedDate;
                            iqci.IpqcNo = param.ipqc.IpqcNo;
                        }
                        DataAccess.Insert(param.ipqc, broker);
                        DataAccess.Insert(param.addRecords, broker);
                        broker.Commit();
                        return true;
                    }
                    else//修改
                    {
                        param.ipqc.UpdatedDate = DateTime.Now;
                        foreach (var iqci in param.addRecords)
                        {
                            iqci.CreatedBy = param.ipqc.CreatedBy;
                            iqci.CreatedDate = param.ipqc.CreatedDate;
                            iqci.IpqcNo = param.ipqc.IpqcNo;
                        }
                        DataAccess.Update(param.ipqc, broker);
                        DataAccess.Delete(param.delRecords, broker);
                        DataAccess.Update(param.updatedRecords, broker);
                        DataAccess.Insert(param.addRecords, broker);
                        broker.Commit();
                        return true;
                    }
                }
                catch (Exception ex)
                {
                    broker.RollBack();
                    return false;
                }
            }
        }

        //PQC，成品质检
        public QcPqcEntity GetQcPqcByKey(string id)
        {
            if (string.IsNullOrEmpty(id))
                return null;
            return DataAccess.SelectSingle<QcPqcEntity>(new QcPqcEntity { Id = id });
        }

        public bool AddQcPqc(QcPqcEntity pqc)
        {
            if (pqc == null)
                return false;
            pqc.CreatedDate = DateTime.Now;
            pqc.UpdatedDate = DateTime.Now;
            pqc.PqcNo = MstCodeRuleBs.GetCurrentOrderCode("QC_PQC");
            return DataAccess.Insert(pqc);
        }

        public bool UpdateQcPqc(QcPqcEntity pqc)
        {
            if (pqc == null)
                return false;
            pqc.UpdatedDate = DateTime.Now;
            return DataAccess.Update(pqc);
        }

        public bool DeleteQcPqc(QcPqcEntity pqc)
        {
            if (pqc == null)
                return false;
            return DataAccess.Delete(pqc);
        }

        public Common.DTO.PqcQueryPageResult GetQcPqcAllListByCondition(Common.DTO.PqcQueryCondition con)
        {
            int pageIndex = 1;
            int pageSize = 50;
            if (con.txtPageIndex != 0)
                pageIndex = con.txtPageIndex;
            if (con.txtPageSize != 0)
                pageSize = con.txtPageSize;
            string condition = "";
            if (!string.IsNullOrEmpty(con.CreatedBy))
            {
                condition += " and pqc.CREATED_BY='" + con.CreatedBy + "'";
            }
            else if (!string.IsNullOrEmpty(con.CreatedUser))
            {
                condition += " and u1.firstName like '%" + con.CreatedUser + "%'";
            }
            condition += " and pqc.IS_ACTIVE=" + con.IsActive;
            if (!string.IsNullOrEmpty(con.PqcNo))
            {
                condition += " and pqc.PQC_NO='" + con.PqcNo + "'";
            }
            if (!string.IsNullOrEmpty(con.WoNo))
            {
                condition += " and pqc.WO_NO='" + con.WoNo + "'";
            }
            if (!string.IsNullOrEmpty(con.ProdPoNo))
            {
                condition += " and pqc.PROD_PO_NO='" + con.ProdPoNo + "'";
            }
            if (!string.IsNullOrEmpty(con.MaterialId))
            {
                condition += " and pqc.MATERIAL_ID='" + con.MaterialId + "'";
            }
            else if (!string.IsNullOrEmpty(con.MaterialName))
            {
                condition += " and pqc.MATERIAL_NAME='" + con.MaterialName + "'";
            }
            if (!string.IsNullOrEmpty(con.Type))
            {
                condition += " and pqc.TYPE='" + con.Type + "'";
            }
            if (!string.IsNullOrEmpty(con.SolutionNo))
            {
                condition += " and pqc.SOLUTION_NO='" + con.SolutionNo + "'";
            }
            if (!string.IsNullOrEmpty(con.SampleStd))
            {
                condition += " and pqc.SAMPLE_STD='" + con.SampleStd + "'";
            }
            if (con.Status != null)
            {
                condition += " and pqc.STATUS='" + con.Status + "'";
            }

            string sql = "";
            if (string.IsNullOrEmpty(condition))
            {
                string sqlSource = @"select top {0} *,(select count(1) from QC_PQC) as cnt
from QC_PQC
where id not in (
    select top ({1}*({2}-1)) id from QC_PQC order by created_date desc
) order by created_date desc";
                sqlSource = string.Format(sqlSource, pageSize, pageSize, pageIndex);
                sql = @"select pqc.*,u1.firstName as CREATED_USER,u2.firstName as UPDATED_USER 
from ({0}) pqc
left join BPMDB.dbo.PW_User u1 on pqc.CREATED_BY=u1.userNo
left join BPMDB.dbo.PW_User u2 on pqc.CREATED_BY=u2.userNo";
                sql = string.Format(sql, sqlSource);
            }
            else
            {
                sql = @"select top {0} *,(select count(1) from ({3}) t) as cnt
from ({3}) t
where t.id not in (
    select top ({1}*({2}-1)) id from ({3}) t order by created_date desc
) order by created_date desc";
                string sqlSource = @"select pqc.*,u1.firstName as CREATED_USER,u2.firstName as UPDATED_USER 
from QC_PQC pqc
left join BPMDB.dbo.PW_User u1 on pqc.CREATED_BY=u1.userNo
left join BPMDB.dbo.PW_User u2 on pqc.CREATED_BY=u2.userNo
where 1=1 ";
                sqlSource += condition;
                sql = string.Format(sql, pageSize, pageSize, pageIndex, sqlSource);
            }
            DataSet ds = DataAccess.SelectDataSet(sql);
            if (ds == null)
                return null;
            PqcQueryPageResult result = new PqcQueryPageResult();
            if (ds.Tables[0].Rows.Count == 0)
            {
                result.results = null;
                result.TotalCount = 0;
                return result;
            }
            List<QcPqcEntity> results = new List<QcPqcEntity>();
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                QcPqcEntity sol = new QcPqcEntity(dr);
                results.Add(sol);
            }
            result.results = results;
            result.TotalCount = (int)ds.Tables[0].Rows[0]["cnt"];
            return result;
        }

        public bool AddQcPqcRecord(QcPqcRecordEntity e)
        {
            if (e == null)
                return false;
            e.CreatedDate = DateTime.Now;
            return DataAccess.Insert(e);
        }

        public bool UpdateQcPqcRecord(QcPqcRecordEntity e)
        {
            if (e == null)
                return false;
            return DataAccess.Update(e);
        }

        public bool DeleteQcPqcRecord(QcPqcRecordEntity e)
        {
            if (e == null)
                return false;
            return DataAccess.Delete(e);
        }

        public List<QcPqcRecordEntity> GetQcPqcRecordAllListByPqcNo(string PqcNo)
        {
            if (string.IsNullOrEmpty(PqcNo))
                return null;
            return DataAccess.Select<QcPqcRecordEntity>(new QcPqcRecordEntity { PqcNo = PqcNo });
        }

        public bool SaveQcPqc(Common.DTO.PqcSaveParam param)
        {
            if (param == null)
                return false;
            if (param.pqc == null)
                return false;
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    QcSpqcEntity cur = DataAccess.SelectSingle<QcSpqcEntity>(new QcSpqcEntity { Id = param.pqc.Id });
                    if (cur == null)//新增
                    {
                        param.pqc.CreatedDate = DateTime.Now;
                        param.pqc.UpdatedDate = DateTime.Now;
                        param.pqc.PqcNo = MstCodeRuleBs.GetCurrentOrderCode("QC_PQC");
                        foreach (var iqci in param.addRecords)
                        {
                            iqci.CreatedBy = param.pqc.CreatedBy;
                            iqci.CreatedDate = param.pqc.CreatedDate;
                            iqci.PqcNo = param.pqc.PqcNo;
                        }
                        DataAccess.Insert(param.pqc, broker);
                        DataAccess.Insert(param.addRecords, broker);
                        broker.Commit();
                        return true;
                    }
                    else//修改
                    {
                        param.pqc.UpdatedDate = DateTime.Now;
                        foreach (var iqci in param.addRecords)
                        {
                            iqci.CreatedBy = param.pqc.CreatedBy;
                            iqci.CreatedDate = param.pqc.CreatedDate;
                            iqci.PqcNo = param.pqc.PqcNo;
                        }
                        DataAccess.Update(param.pqc, broker);
                        DataAccess.Delete(param.delRecords, broker);
                        DataAccess.Update(param.updatedRecords, broker);
                        DataAccess.Insert(param.addRecords, broker);
                        broker.Commit();
                        return true;
                    }
                }
                catch (Exception ex)
                {
                    broker.RollBack();
                    return false;
                }
            }
        }


    }
}
